from FlaskPro import db

class Base(db.Model):
    __abstract__ = True #防止被继承之后创建Base表在数据库
    id = db.Column(db.Integer, primary_key = True, autoincrement = True)

    def save(self):
        session = db.session
        session.add(self)
        session.commit()

    def update(self):
        session = db.session
        session.commit()

    def delete(self):
        session = db.session
        session.delete(self)
        session.commit()

class Department(Base):
    "部门表"
    __tablename__ = "department"
    d_name = db.Column(db.String(32))
    d_description = db.Column(db.Text)

    #构建与职位表外键的反向映射
    d_position = db.relationship(
        "Position",
        backref = "p_dep"
    )
    # 构建与职员表外键的反向映射
    d_person = db.relationship(
        "Person",
        backref="person_deparment"
    )

#权限和职位的多对多中间表
pos_per = db.Table(
    "pos_per",
    db.Column("pos_id",db.Integer,db.ForeignKey("position.id")),
    db.Column("per_id", db.Integer, db.ForeignKey("permission.id"))
)

#权限和职员的多对多中间表
per = db.Table(
    "per",
    db.Column("pos_id",db.Integer,db.ForeignKey("person.id")),
    db.Column("pms_id", db.Integer, db.ForeignKey("permission.id"))
)

class Permission(Base):
    "权限表"
    __tablename__ = "permission"
    p_description = db.Column(db.Text)
    p_table = db.Column(db.String(32))
    p_obj_id = db.Column(db.Integer) # 0 代表整个表 其他正整数代表具体一条数据
    p_type = db.Column(db.String(2)) # a增  d删  u改  s查

class Position(Base):
    "职位表"
    __tablename__ = "position"
    p_name = db.Column(db.String(32))
    p_level = db.Column(db.Integer) #职级

    #构建与部门表进行关联的外键
    p_department = db.Column(db.Integer,db.ForeignKey("department.id"))
    #构建和权限的反向映射
    p_permission = db.relationship(
        "Permission",
        secondary = pos_per,
        backref = "person_position"
    )
    #搭建关联职员的反向映射
    p_person = db.relationship(
        "Person",
        backref = "per_pos" #值不可以和Person表当中的外键字段一样
    )

class Person(Base):
    "职员表"
    __tablename__ = "person"
    per_name = db.Column(db.String(32))
    per_id = db.Column(db.String(8))
    password = db.Column(db.String(32))
    gender = db.Column(db.String(4)) #性别
    age = db.Column(db.Integer)
    per_phone = db.Column(db.String(32))
    per_email = db.Column(db.String(64))
    address = db.Column(db.Text)
    per_photo = db.Column(db.String(128))

    join_time = db.Column(db.Date) #采用当前时间

    #创建关联职位的外键
    per_position = db.Column(db.Integer,db.ForeignKey("position.id"))
    #创建关联部门的外键
    per_department = db.Column(db.Integer,db.ForeignKey("department.id"))
    # 构建职员和权限的反向映射
    p_permission = db.relationship(
        "Permission",
        secondary= per,
        backref="per_pos"
    )
    # 构建和考勤的反向映射
    p_attendance = db.relationship(
        "Attendance",
        backref="a_per"
    )

class Attendance(Base):
    """
    考勤表
        请假类型

	    请假原因
	    请假时间
	    请假时长
	    请假状态
            0 申请中
            1 通过
            2 驳回
            3 销假
	    请假人

    """
    __tablename__ = "attendance"
    a_type = db.Column(db.String(32))
    a_reason = db.Column(db.Text)
    a_time = db.Column(db.Date)
    a_long = db.Column(db.Float) #单位是天
    a_state = db.Column(db.Integer) #请假状态
    verify_person = db.Column(db.Integer) #审核的逻辑是直属领导审核  同一部门，职级大于本人一级的员工

    a_person = db.Column(db.Integer,db.ForeignKey("person.id"))

class New(Base):
    __tablename__ = "new"
    title = db.Column(db.String(32))
    author = db.Column(db.String(32))
    n_time = db.Column(db.Date)
    content = db.Column(db.Text)


